/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * createCustomerUI.java
 *
 * Created on 09-12-2010, 10:27:39
 */
package UILayer.Screens;

import ControlLayer.GlobalInstances;
import DBLayer.DBActor;
import ModelLayer.Actor;
import ModelLayer.Customer;
import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author KL-DELL
 */
public class createCustomerUI extends javax.swing.JPanel
{
    HashMap<Integer, String> zipCodes;

    public createCustomerUI()
    {
        initComponents();

        DBActor dbActor = new DBActor();
        zipCodes = dbActor.GetZipCodes();
        PopulateZipCodes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtCustomerName = new javax.swing.JTextField();
        txtCustomerAddress = new javax.swing.JTextField();
        txtCustomerCountry = new javax.swing.JTextField();
        txtCustomerPhonenumber = new javax.swing.JTextField();
        txtCustomerEmail = new javax.swing.JTextField();
        btnCreateCustomer = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        txtCustomerCity = new javax.swing.JTextField();
        cbZipCode = new javax.swing.JComboBox();
        lblStatus = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("New Customer"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("Navn:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Adresse:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Post Nr.");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Land:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Telefon:");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("E-mail:");
        jLabel6.setName("jLabel6"); // NOI18N

        txtCustomerName.setName("txtCustomerName"); // NOI18N

        txtCustomerAddress.setName("txtCustomerAddress"); // NOI18N

        txtCustomerCountry.setText("Danmark");
        txtCustomerCountry.setName("txtCustomerCountry"); // NOI18N

        txtCustomerPhonenumber.setName("txtCustomerPhonenumber"); // NOI18N

        txtCustomerEmail.setName("txtCustomerEmail"); // NOI18N

        btnCreateCustomer.setText("Opret");
        btnCreateCustomer.setName("btnCreateCustomer"); // NOI18N
        btnCreateCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateCustomerActionPerformed(evt);
            }
        });

        btnCancel.setText("Annuller");
        btnCancel.setName("btnCancel"); // NOI18N

        jLabel7.setText("By");
        jLabel7.setName("jLabel7"); // NOI18N

        txtCustomerCity.setEditable(false);
        txtCustomerCity.setName("txtCustomerCity"); // NOI18N

        cbZipCode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg postnummer" }));
        cbZipCode.setName("cbZipCode"); // NOI18N
        cbZipCode.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbZipCodeItemStateChanged(evt);
            }
        });

        lblStatus.setName("lblStatus"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnCreateCustomer)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblStatus))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCustomerCity, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(txtCustomerName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(txtCustomerAddress, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(cbZipCode, 0, 163, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtCustomerCountry, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                            .addComponent(txtCustomerEmail, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                            .addComponent(txtCustomerPhonenumber, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCustomerPhonenumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCustomerAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCustomerEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCustomerCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cbZipCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCustomerCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreateCustomer)
                    .addComponent(btnCancel)
                    .addComponent(lblStatus))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateCustomerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCreateCustomerActionPerformed
    {//GEN-HEADEREND:event_btnCreateCustomerActionPerformed
        if (cbZipCode.getSelectedIndex() == 0)
        {
            lblStatus.setText("Vælg et postnummer");
            return;
        }

        Actor actor = new Actor(txtCustomerName.getText(), txtCustomerAddress.getText(), txtCustomerPhonenumber.getText(), txtCustomerCountry.getText(), String.valueOf(cbZipCode.getSelectedItem()), txtCustomerCity.getText(), txtCustomerEmail.getText());
        Customer customer = new Customer();
        customer.setTotalSpent(58362);

        actor.addRole(customer);

        try
        {
            GlobalInstances.getInstance().setCurrentCustomer(actor);
        }
        catch (InvalidObjectException ex)
        {
            Logger.getLogger(createCustomerUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnCreateCustomerActionPerformed

    private void cbZipCodeItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_cbZipCodeItemStateChanged
    {//GEN-HEADEREND:event_cbZipCodeItemStateChanged
        if (evt.getStateChange() == evt.SELECTED)
        {
            if (cbZipCode.getSelectedIndex() > 0)
            {
                ArrayList<Integer> codes = new ArrayList<Integer>(zipCodes.keySet());
                String cityName = zipCodes.get(codes.get(cbZipCode.getSelectedIndex() - 1));
                txtCustomerCity.setText(cityName);
            }
        }
    }//GEN-LAST:event_cbZipCodeItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreateCustomer;
    private javax.swing.JComboBox cbZipCode;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JTextField txtCustomerAddress;
    private javax.swing.JTextField txtCustomerCity;
    private javax.swing.JTextField txtCustomerCountry;
    private javax.swing.JTextField txtCustomerEmail;
    private javax.swing.JTextField txtCustomerName;
    private javax.swing.JTextField txtCustomerPhonenumber;
    // End of variables declaration//GEN-END:variables

    private void PopulateZipCodes()
    {
        cbZipCode.removeAllItems();
        ArrayList<Integer> codes = new ArrayList<Integer>(zipCodes.keySet());

        cbZipCode.addItem("Vælg postnummer");

        for (Integer integer : codes)
        {
            cbZipCode.addItem(integer);
        }
    }
}
